﻿using System;
using Microsoft.Xna.Framework;

namespace Firefly.Framework.Input
{
    public class GamePadThumbStickMap : InputMap, IPositionMapEx<Vector2, GamePadManager, GamePadThumbStickMapEventArgs>, IGamePadMap
    {
        public GamePadThumbStick ThumbStick { get; private set; }

        public GamePadThumbStickMap(PlayerIndex playerIndex, GamePadThumbStick thumbStick)
            : this(playerIndex, thumbStick, string.Empty)
        {
        }

        public GamePadThumbStickMap(PlayerIndex playerIndex, GamePadThumbStick thumbStick, string action)
            : base()
        {
            PlayerIndex = playerIndex;
            InputMapType = InputMapType.GamePadThumbStickMap;
            ThumbStick = thumbStick;
            Action = action;
            InputService.Instance.RegisterInputMap(this);
        }

        #region IPositionMap Members

        public Vector2 CurrentPosition
        {
            get
            {
                switch (ThumbStick)
                {
                    case GamePadThumbStick.Left:
                        return Manager.CurrentLeftThumbStickPosition;
                    case GamePadThumbStick.Right:
                        return Manager.CurrentRightThumbStickPosition;
                    default:
                        return Vector2.Zero;

                }
            }
        }
        public Vector2 PreviousPosition
        {
            get
            {
                switch (ThumbStick)
                {
                    case GamePadThumbStick.Left:
                        return Manager.PreviousLeftThumbStickPosition;
                    case GamePadThumbStick.Right:
                        return Manager.PreviousRightThumbStickPosition;
                    default:
                        return Vector2.Zero;
                }
            }
        }
        public Vector2 PositionDelta
        {
            get
            {
                switch (ThumbStick)
                {
                    case GamePadThumbStick.Left:
                        return Manager.LeftThumbStickPositionDelta;
                    case GamePadThumbStick.Right:
                        return Manager.RightThumbStickPositionDelta;
                    default:
                        return Vector2.Zero;
                }
            }
        }

        #endregion

        #region IPositionMapEx Members

        public event EventHandler<GamePadThumbStickMapEventArgs> PositionChanged;

        private void NotifyPositionChanged()
        {
            EventHandler<GamePadThumbStickMapEventArgs> handler = PositionChanged;

            if (handler != null)
            {
                handler(this, new GamePadThumbStickMapEventArgs(this));
            }
        }

        #endregion

        #region IHasInputManager Members

        public GamePadManager Manager { get; set; }

        #endregion

        #region IUpdateable Members

        public override void Update(GameTime gameTime)
        {
            if (Enabled)
            {
                if (PositionDelta != Vector2.Zero)
                {
                    NotifyPositionChanged();
                }
            }
        }

        #endregion

        #region IGamePadMap Members

        public PlayerIndex PlayerIndex { get; private set; }

        #endregion
    }
}
